package com.jsyl.lwbk.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jsyl.lwbk.business.domain.EvaluateRecord;
import com.jsyl.lwbk.business.domain.Question;
import com.jsyl.lwbk.business.mapper.EvaluateRecordMapper;
import com.jsyl.lwbk.business.mapper.EvaluateReportMapper;
import com.jsyl.lwbk.business.mapper.QuestionMapper;
import com.jsyl.lwbk.business.service.IChildQuestionService;
import com.jsyl.lwbk.business.vo.AnswerVO;
import com.jsyl.lwbk.business.vo.ChildQuestionVO;
import com.jsyl.lwbk.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class ChildQuestionServiceImpl implements IChildQuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    EvaluateRecordMapper evaluateRecordMapper;
    @Autowired
    EvaluateReportMapper evaluateReportMapper;

    @Override
    public List<ChildQuestionVO> getChildQuestion(Long childId, String evaluateNumber) {
        List<ChildQuestionVO> list = new ArrayList<>();
        if (StringUtils.isEmpty(evaluateNumber)) {
            List<Question> questionList = questionMapper.selectAll();
            convertQuestionList(list, questionList);
        } else {
            List<EvaluateRecord> evaluateRecordList = evaluateRecordMapper.selectByEvaluateNumber(evaluateNumber);
            convertEvaluateRecord(list, evaluateRecordList);
        }
        return list;
    }

    @Override
    public List<Integer> findProjectModuleCount() {
        return questionMapper.findProjectModuleCount();
    }

    private void convertEvaluateRecord(List<ChildQuestionVO> list, List<EvaluateRecord> evaluateRecordList) {
        for (EvaluateRecord evaluateRecord : evaluateRecordList) {
            ChildQuestionVO vo = new ChildQuestionVO();
            vo.setId(evaluateRecord.getQuestionId());
            vo.setProjectType(evaluateRecord.getProjectType());
            vo.setProjectModule(evaluateRecord.getProjectModule());
            vo.setQuestion(evaluateRecord.getQuestion());
            vo.setQuestionSort(evaluateRecord.getQuestionSort());

//            List<AnswerVO> answerVOList = JSONObject.parseArray(evaluateRecord.getAnswer(), AnswerVO.class);
//            vo.setAnswer(answerVOList);
            vo.setAnswer(answerSort(evaluateRecord.getAnswer()));
            if (evaluateRecord.getChildScore() != null) {
                for (AnswerVO answerVO : vo.getAnswer()) {
                    if (evaluateRecord.getChildScore().equals(answerVO.getScore())) {
                        answerVO.setChecked(true);
                    }
                }
            }
            list.add(vo);
        }
    }

    private void convertQuestionList(List<ChildQuestionVO> childQuestionVOList, List<Question> questionList) {
        for (Question question : questionList) {
            ChildQuestionVO vo = new ChildQuestionVO();
            vo.setId(question.getId());
            vo.setProjectType(question.getProjectType());
            vo.setProjectModule(question.getProjectModule());
            vo.setQuestion(question.getQuestion());
            vo.setQuestionSort(question.getQuestionSort());
            String answer = question.getAnswer();
//            List<AnswerVO> answerVOList = JSONObject.parseArray(answer, AnswerVO.class);
            vo.setAnswer(answerSort(answer));
            childQuestionVOList.add(vo);
        }
    }

    public static List<AnswerVO> answerSort(String jsonArrStr) {
        // json字符串转为JSONArray
        JSONArray jsonArr = JSON.parseArray(jsonArrStr);
        //存放排序结果json数组
        List<AnswerVO> answerVOList = new ArrayList<>();
        // 用于排序的list
        List<JSONObject> list = new ArrayList<JSONObject>();
        //遍历待排序的json数组，并将数据放入list
        for (int i = 0; i < jsonArr.size(); i++) {
            list.add(jsonArr.getJSONObject(i));
        }

        Collections.sort(list, new Comparator<JSONObject>() {
            //排序字段
            private static final String KEY_NAME2 = "score";

            @Override
            public int compare(JSONObject a, JSONObject b) {
                String valB1 = new String();
                String valB2 = new String();
                try {
                    valB1 = a.getString(KEY_NAME2);
                    valB2 = b.getString(KEY_NAME2);
                } catch (JSONException e) {
                    System.out.println(e);
                }
                // 设置排序规则
                int j = valB2.compareTo(valB1);
                return j;
            }
        });
        //将排序后结果放入结果jsonArray
        for (int i = 0; i < jsonArr.size(); i++) {
            answerVOList.add(JSONObject.parseObject(list.get(i).toJSONString(), AnswerVO.class));
        }
        return answerVOList;
    }


}
